package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.BusinessException;
import com.example.domain.Comment;
import com.example.domain.LeaveMessage;
import com.example.domain.vo.CommentVo;
import com.example.domain.vo.CultureProjectCategoryVo;
import com.example.domain.CultureProject;
import com.example.domain.vo.LeaveMessageVo;
import com.example.mapper.CultureCommunityMapper;
import com.example.service.CultureCommunityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CultureCommunityImpl implements CultureCommunityService {

    @Autowired
    private CultureCommunityMapper cultureCommunityMapper;

    @Override
    public PageInfo<CultureProject> queryCulture(CultureProject cultureProject, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<CultureProject> list = cultureCommunityMapper.queryCulture(cultureProject);
        return PageInfo.of(list);
    }

    @Override
    public List<CultureProjectCategoryVo> queryCultureCategory() {
        return cultureCommunityMapper.queryCultureCategory();
    }

    @Override
    public CultureProject queryCultureById(Long id) {
        return cultureCommunityMapper.queryCultureById(id);
    }

    @Override
    public void submitLeaveMessage(LeaveMessage leaveMessage) {
        leaveMessage.setCreateTime(LocalDateTime.now());
        leaveMessage.setUpdateTime(LocalDateTime.now());
        int i = cultureCommunityMapper.submitLeaveMessage(leaveMessage);

        if (i == 0) {
            throw new BusinessException(ResultCodeEnum.SYSTEM_ERROR);
        }
    }

    @Override
    public PageInfo<LeaveMessageVo> queryLeaveMessage(LeaveMessageVo leaveMessageVo, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<LeaveMessageVo> list = cultureCommunityMapper.queryLeaveMessage(leaveMessageVo);
        return PageInfo.of(list);
    }

    @Override
    public void addComments(Comment comment) {
        comment.setCreatedAt(LocalDateTime.now());
        int i = cultureCommunityMapper.addComments(comment);

        if (i == 0) {
            throw new BusinessException(ResultCodeEnum.SYSTEM_ERROR);
        }
    }

    @Override
    public PageInfo<CommentVo> getCommentsByCultureId(Long cultureId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        // 全部评论
        List<CommentVo> comments = cultureCommunityMapper.getCommentsByCultureId(cultureId);
        // 根评论
        List<CommentVo> baseComment = comments.stream().filter(commentVo -> ObjectUtil.isNull(commentVo.getPid())).collect(Collectors.toList());
        // 过滤出根评论的子评论
        for (CommentVo commentVo : baseComment) {
            commentVo.setChildren(comments.stream().filter(commentVo1 -> commentVo.getId().equals(commentVo1.getPid())).collect(Collectors.toList()));
        }
        return PageInfo.of(baseComment);
    }

    @Override
    public void removeComments(Long commentId) {
        int i = cultureCommunityMapper.removeComments(commentId);

        if (i == 0) {
            throw new BusinessException(ResultCodeEnum.SYSTEM_ERROR);
        }
    }

    @Override
    public void updateLike(Long commentId, Long likes) {
        Comment comment = cultureCommunityMapper.queryCommentById(commentId);
        Comment comment1 = new Comment();
        comment1.setId(commentId);
        // 1：点赞；0：取消
        if (likes == 1) {
            if (ObjectUtil.isNotEmpty(comment.getLikes())) {
                comment1.setLikes(comment.getLikes() + 1);
                cultureCommunityMapper.updateComment(comment1);
            } else {
                comment1.setLikes(1L);
                cultureCommunityMapper.updateComment(comment1);
            }
        } else {
            if (ObjectUtil.isNotEmpty(comment.getLikes())) {
                comment1.setLikes(comment.getLikes() - 1);
                cultureCommunityMapper.updateComment(comment1);
            }
        }
    }

    @Override
    public void updateTread(Long commentId, Long treader) {
        Comment comment = cultureCommunityMapper.queryCommentById(commentId);
        Comment comment1 = new Comment();
        comment1.setId(commentId);
        // 1：点赞；0：取消
        if (treader == 1) {
            if (ObjectUtil.isNotEmpty(comment.getTread())) {
                comment1.setTread(comment.getTread() + 1);
                cultureCommunityMapper.updateComment(comment1);
            } else {
                comment1.setTread(1L);
                cultureCommunityMapper.updateComment(comment1);
            }
        } else {
            if (ObjectUtil.isNotEmpty(comment.getTread())) {
                comment1.setTread(comment.getTread() - 1);
                cultureCommunityMapper.updateComment(comment1);
            }
        }
    }

    @Override
    public CommentVo getCommentsMaxLikes(Long cultureId) {
        return cultureCommunityMapper.getCommentsMaxLikes(cultureId);
    }
}
